package net.coding.api.test.helper;

import io.restassured.http.ContentType;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import net.coding.api.test.TestClient;
import net.coding.api.test.TestHelper;
import net.coding.api.test.common.ProjectInfo;

import java.util.HashMap;
import java.util.Map;

import static io.restassured.path.json.JsonPath.with;
import static net.coding.api.test.ApiMethod.GET;
import static net.coding.api.test.TestClient.client;
import static net.coding.api.test.TestHelper.projectHelper;
import static net.coding.api.test.TestHelper.randomContent;
import static net.coding.api.test.TestHelper.userHelper;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertThat;

/**
 * Created with IntelliJ IDEA.
 * User: chenfeilong
 * Date: 2018/1/18
 * Time: 11:44
 * Description: 项目相关操作
 */
public interface BaseProjectHelper {

    /**
     * 创建项目
     *
     * @param type
     * @param name
     * @param description
     * @param gitEnabled
     * @param gitReadmeEnabled
     * @param gitIgnore
     * @param gitLicense
     * @param vcsType
     */
    Response createProject(int userIndex, int type, String name, String description, boolean gitEnabled,
                           boolean gitReadmeEnabled, String gitIgnore, String gitLicense, String vcsType);


    /**
     * 获取项目信息
     *
     * @param projectName
     * @return
     */
    default JsonPath getProjectInfo(String projectName) {
        return getProjectInfoResponse(projectName).body().jsonPath();
    }

    default Response getProjectInfoResponse(String projectName) {
        return TestClient.client().login().request(GET, getProjectURI(projectName)).returnedCode(0)
                .response();
    }

    default Object getProjectInfo(String projectName, String filed) {
        return getProjectInfo(projectName).get(filed);
    }

    /**
     * 获取项目ID
     *
     * @param projectName
     * @return
     */

    default Integer getProjectId(String projectName) {
        JsonPath path = getProjectInfo(projectName);
        return path.getInt("data.id");
    }

    default String getSvnUrl(String projectName) {
        JsonPath path = getProjectInfo(projectName);
        return path.getString("data.svn_url").replace("svn+ssh", "svn").replace("svn@", "");
    }

    /**
     * 获取可用项目名
     *
     * @return 可用项目名
     */
    default String getAvailableProjectName() {
        String newProjectName;
        while (true) {
            newProjectName = TestHelper.randomProjectName();
            int code = client().login()
                    .request(GET, getProjectURI(newProjectName))
                    .response().body().jsonPath()
                    .getInt("code");
            //项目不存在
            if (code == 1100) {
                break;
            }
        }
        return newProjectName;
    }


    /**
     * 快速创建项目
     *
     * @param userIndex
     * @param type
     * @return
     */
    default ProjectInfo fastCreateProject(int userIndex, int type) {
        String projectName = projectHelper.getAvailableProjectName();
        projectHelper.createProject(userIndex, type, projectName, randomContent(), true
                , true, "no", "no", "git");
        Integer projectId = projectHelper.getProjectId(projectName);
        return new ProjectInfo(projectId, projectName);
    }


    /**
     * 删除项目
     *
     * @param userIndex   用户索引
     * @param projectInfo 项目信息
     */
    Response deleteProject(int userIndex, ProjectInfo projectInfo);

    /**
     * 获取项目创建参数
     *
     * @param type
     * @param name
     * @param description
     * @param gitEnabled
     * @param gitReadmeEnabled
     * @param gitIgnore
     * @param gitLicense
     * @param vcsType
     * @return
     */
    default Map<String, Object> getCreateProjectParams(int type, String name, String description, boolean gitEnabled
            , boolean gitReadmeEnabled, String gitIgnore, String gitLicense, String vcsType) {
        return new HashMap<String, Object>() {
            private static final long serialVersionUID = 1L;

            {
                put("type", type);
                put("name", name);
                put("description", description);
                put("gitEnabled", gitEnabled);
                put("gitReadmeEnabled", gitReadmeEnabled);
                put("gitIgnore", gitIgnore);
                put("gitLicense", gitLicense);
                put("vcsType", vcsType);
            }
        };
    }


    Integer getProjectCreatorRole();

    String getProjectPath(String projectName);

    default String getProjectURI(String projectName) {
        return String.format("/user/%s/project/%s", userHelper.getGlobalKey(), projectName);
    }

     String getBackendProjectPath(String projectName);

    default void checkReturnedCode(Response resp, int expectedCode) {
        resp.then().assertThat().statusCode(200).contentType(ContentType.JSON);

        JsonPath path = with(resp.asString());
        int returnedCode = path.getInt("code");
        String msg = path.get("msg") == null ? "null" : path.getMap("msg").toString();
        assertThat(msg, returnedCode, equalTo(expectedCode));
    }

}
